package io.xxx.omni.mdc.util

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import java.text.DecimalFormat
import java.text.NumberFormat
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.*

val numberFormat: NumberFormat = DecimalFormat("0.00")

val zoneOffset: ZoneOffset = OffsetDateTime.now().offset

val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

fun String?.toLocalDateTime(): LocalDateTime? {
    if (this == null) {
        return null
    }
    return LocalDateTime.parse(this, dateTimeFormatter)
}

fun Long?.toLocalDateTime(): LocalDateTime? {
    if (this == null) {
        return null
    }
    return LocalDateTime.ofEpochSecond(this / 1000, (this % 1000).toInt(), zoneOffset)
}

fun LocalDateTime.toDate(): Date {
    return Date(this.toInstant(zoneOffset).toEpochMilli())
}

fun Date.toLocalDateTime(): LocalDateTime {
    return LocalDateTime.ofInstant(this.toInstant(), zoneOffset)
}

fun Any.toJSONObject(): JSONObject {
    return JSON.parseObject(JSON.toJSONString(this))
}